/* nixonFTP
 * FTP client version 0.1
 * Copyright (C) 2010 NIXON Development Corporation.
 * All rights reserved.
 * http://members.shaw.ca/nixon.com
 */
package nixonftp.ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.JToggleButton;
import javax.swing.JViewport;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

/**
 *
 * @author Billy
 */
public class NXSubFrame extends JDialog implements MouseListener, MouseMotionListener {

	private JTextPane a;
	private JLabel b;
	private JScrollPane spa;
	private JScrollPane spb;
	private ImageIcon i;

	private int initialX;
	private int initialY;
	private static boolean down;
	private JViewport v;
	private int initialLeft;

	private boolean addedImage;
	private JToggleButton tool;

	public NXSubFrame(Frame owner, String title, boolean image, ImageIcon img) {
		super(owner);
		setTitle(title);
		setUp(image, img);
		(this.getContentPane()).setPreferredSize(new Dimension(180, 180));
	}

	public void setUp(boolean image, ImageIcon img) {
		a = new JTextPane();
		//a.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
		addStyles(a.getStyledDocument());
		a.setEditable(false);
		a.setBackground(Color.WHITE);
		a.setOpaque(true);

		if (!(b instanceof JLabel)) {
			b = new JLabel(img);
		} else {
			if (img != null) setImage(img);
		}
		b.setBackground(new Color(255, 255, 255));
		spa = new JScrollPane(a);
		spb = new JScrollPane(b);
		b.addMouseListener(this);
		b.addMouseMotionListener(this);
		v = (JViewport)b.getParent();
		if (image) {
			add(spb);
		} else {
			add(spa);
		}
		addedImage = image;
		//add(spb);
		spa.setBorder(null);
		spb.setBorder(null);

		spa.setVisible(!image);
		spb.setVisible(image);
		pack();
	}

	public void setImage(ImageIcon img) {
		if (b instanceof JLabel) {
			remove(spa);
			add(spb);
			i = img;
			b.setIcon(img);
			spb.setVisible(true);
			spa.setVisible(false);
			(this.getContentPane()).setPreferredSize(new Dimension(img.getIconWidth(), img.getIconHeight()));
			pack();
		}
	}

	private void addStyles(StyledDocument doc) {
		Style def = StyleContext.getDefaultStyleContext().
				  getStyle(StyleContext.DEFAULT_STYLE);

		Style command = doc.addStyle("command", def);
		StyleConstants.setFontFamily(def, "Monospaced");
		StyleConstants.setForeground(command, Color.BLACK);

		//responses

		Style regular = doc.addStyle("regular", command);
		StyleConstants.setForeground(regular, Color.GRAY);

		Style s = doc.addStyle("tryagain", command);
		StyleConstants.setForeground(s, Color.ORANGE);

		s = doc.addStyle("pending", command);
		StyleConstants.setForeground(s, new Color(51, 102, 153));

		s = doc.addStyle("error", command);
		StyleConstants.setForeground(s, Color.RED);

		s = doc.addStyle("done", command);
		StyleConstants.setForeground(s, new Color(0, 153, 0));
	}

	public JTextPane getLog() {
		return a;
	}

	public void loadImage(String file) {
		b.setIcon(new ImageIcon(file));
		repaint();
	}

	public void displayPlainText(String s) {
		a.setText(s);
		setUpText();
	}

	private void setUpText() {
		remove(spb);
		add(spa);
		i = null;
		spa.setVisible(true);
		spb.setVisible(false);
		pack();
	}

	public void displayText(Vector<String> s) {
		if (s == null) {
			a.setText("");
			return;
		}

		Object[] lines = s.toArray();
		String line;
		
		boolean eraseControl = false;
		
		try {
			for (int x = 0; x < lines.length; x++) {
				line = (String) lines[x];
				appendText(line);
			}
		} catch (Exception e) {
		}

		setUpText();
	}

	public void appendText(String text) {
		String style;
		boolean eraseControl;
		StyledDocument doc = a.getStyledDocument();
		if (text.startsWith("\u3000")) {
			style = "error";
			eraseControl = true;
		} else if (text.startsWith("\u3001")) {
			style = "tryagain";
			eraseControl = true;
		} else if (text.startsWith("\u3002")) {
			style = "pending";
			eraseControl = true;
		} else if (text.startsWith("\u3003")) {
			style = "done";
			eraseControl = true;
		} else if (text.startsWith("\u3004")) {
			style = "regular";
			eraseControl = true;
		} else {
			style = "";
			eraseControl = false;
		}

		if (eraseControl) {
			text = text.substring(1);
		}
		try {
			doc.insertString(doc.getLength(), text + '\n', doc.getStyle(style));
		} catch (BadLocationException ex) {
		}
	}

	public void mouseClicked(MouseEvent e) {
		down = false;
	}

	public void mousePressed(MouseEvent e) {
		initialX = e.getX();
		initialY = e.getY();
		down = true;
	}

	public void mouseReleased(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	public void mouseDragged(MouseEvent e) {
		if (!down) return;
		boolean doX;
		int diffX = e.getX() - initialX;
		int diffY = e.getY() - initialY;

		int x = v.getViewPosition().x - diffX;
		int y = v.getViewPosition().y - diffY;

		int end = v.getView().getSize().width - v.getSize().width;
		if (x > end) x = end;
		end =  v.getView().getSize().height - v.getSize().height;
		if (y > end) y = end;
		if (x < 0) x = 0;
		if (y < 0) y = 0;
		if (b.getWidth() <= v.getSize().width) x = v.getViewPosition().x;
		if (b.getHeight() <= v.getSize().height) y = v.getViewPosition().y;
		
		v.setViewPosition(new Point(x, y));
	}

	public void mouseMoved(MouseEvent e) {
		
	}

	public void setTool(JToggleButton btn) {
		tool = btn;
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				tool.setSelected(false);
			}
		});
	}
}
